﻿using ExcelSystem.Model;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using System.Threading;
using System;
using System.Windows;
using ExcelSystem.Event;
using ExcelSystem.Views;

namespace ExcelSystem.ViewModels
{
    public class MainWindowViewModel : BindableBase
    {
        ExcelContext db;
        private string _title = "上丰管理系统";
        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title, value); }
        }

        private string warnInfo;

        /// <summary>
        /// 提示信息
        /// </summary>
        public string WarnInfo
        {
            get { return warnInfo; }
            set { SetProperty(ref warnInfo, value); }
        }


        private bool warnActive;

        /// <summary>
        /// 提示框显示
        /// </summary>
        public bool WarnActive
        {
            get { return warnActive; }
            set { SetProperty(ref warnActive, value); }
        }


        private DelegateCommand<string> menuOpenCommand;
        public DelegateCommand<string> MenuOpenCommand
        {
            get { return menuOpenCommand; }
            set { SetProperty(ref menuOpenCommand, value); }
        }
        Timer timer;
        private readonly IDialogService dialog;
        private readonly IRegionManager regionManage;
        private readonly IEventAggregator eventAggregator;
        private readonly IRegionNavigationJournal journal;
        public MainWindowViewModel(IDialogService dialog, IRegionManager regionManage, IEventAggregator eventAggregator, IRegionNavigationJournal journal)
        {
            this.dialog = dialog;
            this.regionManage = regionManage;
            this.eventAggregator = eventAggregator;
            this.journal = journal;
            MenuOpenCommand = new DelegateCommand<string>(MenuOpen);
            regionManage.RegisterViewWithRegion("ContentRegion", typeof(OrderList));
            eventAggregator.GetEvent<GetInputMessages>().Subscribe(GetWarnInfo);
            //timer = new Timer(ChangeWarnActive, null, 1500, 1500);



        }

        private void ChangeWarnActive(object state)
        {
            if (WarnActive == true)
                Application.Current.Dispatcher.Invoke(new Action(() =>
                {
                    WarnActive = false;
                }));

        }

        private void GetWarnInfo(string obj)
        {
            WarnInfo = obj;
            WarnActive = true;
            Thread thread = new Thread(() => {
                Thread.Sleep(5000);
                WarnActive = false;
            })
            {
                IsBackground = true
            };
            thread.Start();



        }


        private void MenuOpen(string obj)
        {
            switch (obj)
            {
                case "Order":
                    regionManage.RequestNavigate("ContentRegion", "OrderList");
                    break;
                case "BOM":
                    regionManage.RequestNavigate("ContentRegion", "BOMList");
                    break;
                case "BOMYM":
                    regionManage.RequestNavigate("ContentRegion", "BOMListYM");
                    break;
            }

        }
    }
}
